import _ from 'lodash';
import { getDeepObjectProperty } from '@/utils/obj';
import hasFill from './setting/hasFill';

export default {
  data() {
    return {
      // field_label field_value
      option: [],

      // 选择value
      valueList: []
    };
  },

  mixins: [hasFill],

  props: {
    choice: {
      type: Array,
      default: () => null
    },
    defaultProps: {
      type: Object,
      default: () => ({})
    }
  },

  watch: {
    choice: {
      immediate: true,
      handler(val) {
        // 远程
        if (this.remote) {
          this.fetchRemoteChoice && this.fetchRemoteChoice();
        } else {
          const { data } = this.defaultProps;
          const { label, value, has_fill } = data;

          const isProperty = (o) => !!getDeepObjectProperty(has_fill, o);

          const { type, fill } = this.collectProps;
          this.option = _.map(
            val, (o) => {
              const v = o[value];
              const index = this.$_
                .findIndex(this.valueList, (vl) => (vl[type] === v));

              return {
                field_value: o[value],
                field_label: o[label],
                has_fill: isProperty(o),
                fill_value: (index > -1) ? this.valueList[index][fill] : '',
                prop: this.createInput({ fields: o })
              };
            }
          );
          this.filterOption = this.option;
          console.log(this.option, 'dsdsds');
        }
      }
    }
  }
};
